package com.shineoxygen.work.temp.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shineoxygen.common.base.alipay.directpay.config.AlipayConfig;
import com.shineoxygen.common.base.alipay.directpay.model.Alipay;
import com.shineoxygen.common.base.alipay.directpay.utils.AlipaySubmit;
import com.shineoxygen.common.base.controller.BaseAdminController;

/**
 * @author 王辉阳
 * @date 2016年10月24日 下午5:13:59
 * @Description 支付宝即时到账通知
 */
@Controller
@RequestMapping("/aliDirectPay")
public class AliDirectPayController extends BaseAdminController {
	private static final Logger logger = LogManager.getLogger(AliDirectPayController.class);

	@RequestMapping(value = "/payPage")
	public String payPage() {
		return "tempTest/alipay/index";
	}

	@RequestMapping(value = "/payQueryPage")
	public String payQueryPage() {
		return "tempTest/alipay/payQuery";
	}

	/**
	 * 支付页面（扫码或在线登陆选择页）
	 * 
	 * @param out_trade_no
	 *            商户订单号，商户网站订单系统中唯一订单号，必填
	 * @param subject
	 *            订单名称，必填
	 * @param total_fee
	 *            付款金额，必填
	 * @param body
	 *            商品描述，可空
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/pay")
	public String pay(@RequestParam("WIDout_trade_no") String out_trade_no, @RequestParam("WIDsubject") String subject, @RequestParam("WIDtotal_fee") String total_fee,
			@RequestParam("WIDbody") String body, HttpServletResponse response, ModelMap modelMap) throws IOException {
		// 把请求参数打包成数组
		Map<String, String> sParaTemp = new HashMap<String, String>();
		sParaTemp.put("service", AlipayConfig.service);
		sParaTemp.put("partner", AlipayConfig.partner);
		sParaTemp.put("seller_id", AlipayConfig.seller_id);
		sParaTemp.put("_input_charset", AlipayConfig.input_charset);
		sParaTemp.put("payment_type", AlipayConfig.payment_type);
		sParaTemp.put("notify_url", AlipayConfig.notify_url);
		sParaTemp.put("return_url", AlipayConfig.return_url);
		sParaTemp.put("anti_phishing_key", AlipayConfig.anti_phishing_key);
		sParaTemp.put("exter_invoke_ip", AlipayConfig.exter_invoke_ip);
		sParaTemp.put("out_trade_no", out_trade_no);
		sParaTemp.put("subject", subject);
		sParaTemp.put("total_fee", total_fee);
		sParaTemp.put("body", body);

		// 建立支付请求：写js自动表单提交
		modelMap.addAttribute("form", AlipaySubmit.buildRequest(sParaTemp, "get", "确认"));
		return "tempTest/alipay/alipayapi";
	}

	/**
	 * 查询支付订单信息
	 * 
	 * @param trade_no
	 * @param out_trade_no
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/payQuery")
	public @ResponseBody Alipay pay(@RequestParam("WIDtrade_no") String trade_no, @RequestParam("WIDout_trade_no") String out_trade_no, HttpServletResponse response,
			ModelMap modelMap) throws IOException {
		// 把请求参数打包成数组
		Map<String, String> sParaTemp = new HashMap<String, String>();
		sParaTemp.put("service", "single_trade_query");
		sParaTemp.put("partner", AlipayConfig.partner);
		sParaTemp.put("_input_charset", AlipayConfig.input_charset);
		sParaTemp.put("trade_no", trade_no);
		sParaTemp.put("out_trade_no", out_trade_no);

		Alipay pay = null;
		try {
			pay = AlipaySubmit.payQuery("", "", sParaTemp);
			System.out.println(pay.getResponse().getTrade().getTrade_status());
			logger.info(pay);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return pay;
	}

}
